package dao.impl;

import bean.Admin;
import bean.User;
import dao.AdminDao;
import util.DBCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    public List<Admin> getAll() {
        List<Admin> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBCon.getConnection();
            preparedStatement = connection.prepareStatement("select * from table_admin");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Admin admin = new Admin();
                admin.setId(resultSet.getInt("id"));
                admin.setAdmin_name(resultSet.getString("admin_name"));
                admin.setPassword(resultSet.getString("password"));
                list.add(admin);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return list;
    }

    public Admin getAdminByAdminname(String admin_name) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBCon.getConnection();
            preparedStatement = connection.prepareStatement("SELECT * FROM table_admin WHERE admin_name = ?");
            preparedStatement.setString(1, admin_name);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Admin admin = new Admin();
                admin.setId(resultSet.getInt("id"));
                admin.setAdmin_name(resultSet.getString("admin_name"));
                admin.setPassword(resultSet.getString("password"));
                return admin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }

}
